<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-- 
    Document   : index
    Created on : 18-gen-2015, 10.24.17
    Author     : Jov
--%>

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<% 
    if(session.getAttribute("user")==null){
        out.println("<meta http-equiv='refresh' content='0; url=login.jsp'>");
    }
    if(session.getAttribute("year")==null) {
        session.setAttribute("year", 2015);
    } 
    if(session.getAttribute("month")==null) {
        session.setAttribute("month", 1);
        session.setAttribute("offset", 3);
    }
    int numberMonth = (Integer) session.getAttribute("month");
   
    if(request.getParameter("month") != null) {
        if(request.getParameter("month").contains("previous")) {
            session.setAttribute("month", numberMonth - 1);
            if(session.getAttribute("month").toString().equals("0")){
                session.setAttribute("month", 12);
                int numberYear = (Integer) session.getAttribute("year");
                session.setAttribute("year", numberYear - 1);
            }
        }
    }
    if(request.getParameter("month") != null) {
        if(request.getParameter("month").contains("next")) {
            session.setAttribute("month", numberMonth + 1);
            if(session.getAttribute("month").toString().equals("13")){
                session.setAttribute("month", 1);
                int numberYear = (Integer) session.getAttribute("year");
                session.setAttribute("year", numberYear + 1);
            }
        }
    }
%>
<sql:query var="resultEvents" dataSource="jdbc/MeteoCalBETA">
    <% 
        out.println("SELECT * FROM events WHERE event_year = "+session.getAttribute("year")+" AND event_month = "+session.getAttribute("month")+" AND creator = "+session.getAttribute("user_id")+"");
    %>
</sql:query>
   
<table border="1">
    <!-- column headers -->
    <tr>
    <c:forEach var="columnName" items="${resultEvents.columnNames}">
        <th><c:out value="${columnName}"/></th>
    </c:forEach>
</tr>
<!-- column data -->
<c:forEach var="row" items="${resultEvents.rowsByIndex}">
    <tr>
    <c:forEach var="column" items="${row}">
        <td><c:out value="${column}"/></td>
    </c:forEach>
    </tr>
</c:forEach>
</table>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>
            MeteoCal BETA		
        </title>	
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <script src="lib/jquery-1.10.1.min.js"></script>
        <script src="lib/jquery-ui.js"></script>
        <script src="js/code.js"></script>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <link rel="stylesheet" type="text/css" href="css/header.css">
    </head>
    <body>
        <div id="titleprint">MeteoCal BETA</div>
        <header>
            <div id="logo">
                <h1>MeteoCal BETA</h1>
                <h2>Meteo-Calendario online</h2>
            </div>
            <nav>
                <ul>
                    <li id="buttonFAQ">
                        FAQ
                    </li>
                    <li id="buttonContact">
                        Contact
                    </li>
                    <li id="buttonPersonalProfile">
                        Personal profile
                    </li>
                    <li id="buttonLogout" >
                        <a href="login.jsp">Logout</a>
                    </li>
                </ul>
            </nav>
        </header>
        <table width="100%">
            <tbody><tr>
                    <td id="yeartitle">
                        <h2><% out.println(session.getAttribute("month")); %></h2><h1><% out.println(session.getAttribute("year")); %></h1></td>
                    <td id="buttons">
                        <a href="?month=previous"><button> &lt;&lt; </button></a>
                        <a href="?month=next"><button> &gt;&gt; </button></a>
                    </td>
                </tr>
            </tbody></table>
        <div style="clear:both;"></div>
        <div id="calendar">
            <%
                try {
                    String connectionURL = "jdbc:mysql://localhost:3306/meteocaldb";
                    Connection connection = null;
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    connection = DriverManager.getConnection(connectionURL, "root", "0000");
                    
                    Statement stmt = connection.createStatement();
                    ResultSet rset;
                    
                    int totDays = 0;
                    
                    if(session.getAttribute("year").toString().equals("2015")){
                        if(session.getAttribute("month").toString().equals("1")) {session.setAttribute("offset", 3);}
                        if(session.getAttribute("month").toString().equals("2")) {session.setAttribute("offset", 6);}
                        if(session.getAttribute("month").toString().equals("3")) {session.setAttribute("offset", 6);}
                        if(session.getAttribute("month").toString().equals("4")) {session.setAttribute("offset", 2);}
                        if(session.getAttribute("month").toString().equals("5")) {session.setAttribute("offset", 4);}
                        if(session.getAttribute("month").toString().equals("6")) {session.setAttribute("offset", 0);}
                        if(session.getAttribute("month").toString().equals("7")) {session.setAttribute("offset", 2);}
                        if(session.getAttribute("month").toString().equals("8")) {session.setAttribute("offset", 5);}
                        if(session.getAttribute("month").toString().equals("9")) {session.setAttribute("offset", 1);}
                        if(session.getAttribute("month").toString().equals("10")) {session.setAttribute("offset", 3);}
                        if(session.getAttribute("month").toString().equals("11")) {session.setAttribute("offset", 6);}
                        if(session.getAttribute("month").toString().equals("12")) {session.setAttribute("offset", 1);}
                    }
                    if(session.getAttribute("year").toString().equals("2014")){
                        if(session.getAttribute("month").toString().equals("1")) {session.setAttribute("offset", 2);}
                        if(session.getAttribute("month").toString().equals("2")) {session.setAttribute("offset", 5);}
                        if(session.getAttribute("month").toString().equals("3")) {session.setAttribute("offset", 5);}
                        if(session.getAttribute("month").toString().equals("4")) {session.setAttribute("offset", 1);}
                        if(session.getAttribute("month").toString().equals("5")) {session.setAttribute("offset", 3);}
                        if(session.getAttribute("month").toString().equals("6")) {session.setAttribute("offset", 6);}
                        if(session.getAttribute("month").toString().equals("7")) {session.setAttribute("offset", 1);}
                        if(session.getAttribute("month").toString().equals("8")) {session.setAttribute("offset", 4);}
                        if(session.getAttribute("month").toString().equals("9")) {session.setAttribute("offset", 0);}
                        if(session.getAttribute("month").toString().equals("10")) {session.setAttribute("offset", 2);}
                        if(session.getAttribute("month").toString().equals("11")) {session.setAttribute("offset", 5);}
                        if(session.getAttribute("month").toString().equals("12")) {session.setAttribute("offset", 0);}
                    }
                    
                    int offset = (Integer) session.getAttribute("offset");
                    
                    if(session.getAttribute("month").toString().equals("1")) {totDays = 31;}
                    if(session.getAttribute("month").toString().equals("2")) {totDays = 28;}
                    if(session.getAttribute("month").toString().equals("3")) {totDays = 31;}
                    if(session.getAttribute("month").toString().equals("4")) {totDays = 30;}
                    if(session.getAttribute("month").toString().equals("5")) {totDays = 31;}
                    if(session.getAttribute("month").toString().equals("6")) {totDays = 30;}
                    if(session.getAttribute("month").toString().equals("7")) {totDays = 31;}
                    if(session.getAttribute("month").toString().equals("8")) {totDays = 31;}
                    if(session.getAttribute("month").toString().equals("9")) {totDays = 30;}
                    if(session.getAttribute("month").toString().equals("10")) {totDays = 31;}
                    if(session.getAttribute("month").toString().equals("11")) {totDays = 30;}
                    if(session.getAttribute("month").toString().equals("12")) {totDays = 31;}
                   
                    for (int day = 1; day <= offset; day++) {
                        out.println("<div class='day ui-droppable' day='' style='position:relative;'><div class='numberday'><b></b></div>");
                        out.println("</div>");
                    }
                    
                    for (int day = 1; day <= totDays; day++) {
                        out.println("<div class='day ui-droppable' day='" + day + "' style='position:relative;'><div class='numberday'><b>" + day + "</b></div>");
                        String query = "SELECT * FROM events WHERE event_year = "+session.getAttribute("year")+" AND event_month = "+session.getAttribute("month")+" AND event_day = " + day + " AND creator = "+session.getAttribute("user_id")+";";
                        rset = stmt.executeQuery(query);
                        while (rset.next()) {
                            out.println("<div event_id='"+rset.getString("event_id")+"' idcalday='7' year='2014' month='9' day='13' hour='9' name='IMF' class='event ui-draggable' style='background:rgb(255,200,255);width:90%;height:" + (rset.getInt("duration") * 0.1) + "%;position:absolute;top:" + ((rset.getInt("event_hour") * 4) + 12) + "%;'>" + rset.getInt("event_hour") + "<span class='motivoevento'>-" + rset.getString("event_name") + "</span><div class='delete'><a style='color:#d7d7d7;' href='actions/deleteEvent.jsp?event_id="+rset.getString("event_id")+"'>X</a></div></div>");
                        }
                        out.println("</div>");
                    }
                    
                    stmt.close();
                    connection.close();
                    
                } catch (Exception ex) {
                    out.println("Errore! " + ex.getMessage().toString());
                }

            %>
        </div>
        <div id="newEvent" style="display: none;">
            <div id="contentform">
                <div id="closeNewEvent">X</div>
                <form action="actions/newEvent.jsp" method="POST">
                    <% 
                        out.println("<input type='hidden' name='year' value="+session.getAttribute("year")+">");
                        out.println("<input type='hidden' name='month' value="+session.getAttribute("month")+">");
                    %>
                    <h3>New event</h3>
                    <table width="100%" id="newEventTable" cellpadding="0" cellspacing="0">
                        <tbody>
                            <tr>
                                <td>
                                    Year:
                                </td>
                                <td>
                                    <% 
                                        out.println(session.getAttribute("year"));
                                    %>							
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Month:	
                                </td>
                                <td>
                                    <% 
                                        out.println(session.getAttribute("month"));
                                    %>						
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Day:
                                </td>
                                <td>
                                    <input type="text" name="day" id="tellMeDay">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Desired weather:
                                </td>
                                <td>
                                    <select name="deiredWeather">
                                       <option value="Sunny">Sunny</option><option value="Cloudy">Cloudy</option><option value="Rainy">Rainy</option><option value="Snowy">Snowy</option>
                                    </select>
                                    <button id="buttonSetDesiredWeather">Set details</button>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Begin time:
                                </td>
                                <td>
                                    <select name="hour">
                                        <%         
                                            for (int hour = 0; hour < 24; hour++) {
                                                out.println("<option value='" + hour + "'>" + hour + "</option>");
                                            }
                                        %>
                                    </select>:

                                    <select name="minute">
                                        <%    
                                            for (int minute = 0; minute < 60; minute += 5) {
                                                out.println("<option value='" + minute + "'>" + minute + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Duration:
                                </td>
                                <td>
                                    <select name="duration">
                                        <option value="0">0</option><option value="30">0:30</option><option value="60">1</option><option value="90">1:30</option><option value="120">2</option><option value="150">2:30</option><option value="180">3</option><option value="210">3:30</option><option value="240">4</option><option value="270">4:30</option><option value="300">5</option><option value="330">5:30</option><option value="360">6</option><option value="390">6:30</option><option value="420">7</option><option value="450">7:30</option><option value="480">8</option><option value="510">8:30</option><option value="540">9</option><option value="570">9:30</option><option value="600">10</option><option value="630">10:30</option><option value="660">11</option><option value="690">11:30</option><option value="720">12</option><option value="750">12:30</option><option value="780">13</option><option value="810">13:30</option><option value="840">14</option><option value="870">14:30</option><option value="900">15</option><option value="930">15:30</option><option value="960">16</option><option value="990">16:30</option><option value="1020">17</option><option value="1050">17:30</option><option value="1080">18</option><option value="1110">18:30</option><option value="1140">19</option><option value="1170">19:30</option><option value="1200">20</option><option value="1230">20:30</option><option value="1260">21</option><option value="1290">21:30</option><option value="1320">22</option><option value="1350">22:30</option><option value="1380">23</option><option value="1410">23:30</option>						</select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Name of the event:
                                </td>
                                <td>
                                    <input type="text" name="name" placeholder="name">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Location:
                                </td>
                                <td>
                                    <input type="text" name="location" placeholder="location">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Private?
                                </td>
                                <td>
                                    <select name="privacy">
                                        <option value="1">Yes</option><option value="0">No</option>
                                    </select>
                                </td>  
                            </tr>
                            <tr>
                                <td>
                                    Shared?
                                </td>
                                <td>
                                    <select name="shared">
                                        <option value="1">Yes</option><option value="0">No</option>
                                    </select>
                                </td>  
                            </tr>
                            <tr>
                                <td>

                                </td>
                                <td>
                                    <input type="submit">
                                </td>
                            </tr>
                        </tbody></table>	
                </form>
            </div>
        </div>
        <%-- ///////////////////////////////////////// --%>
        <div id="setDesiredWeather" style="display: none;">
            <div id="contentform">
                <div id="closeSetDesiredWeather">X</div>
                <form>
                    <h3>Set details about desired weather:</h3>
                    <table>
                        <tbody>
                            <tr>
                                <td>
                                    Minimum desired temperature (°C):
                                    <select name="minDesiredTemp">
                                        <%         
                                            for (int minTemp = -10; minTemp <= 40; minTemp+=5) {
                                                out.println("<option value='" + minTemp + "'>" + minTemp + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                                <td>
                                    Maximum desired temperature (°C):
                                    <select name="maxDesiredTemp">
                                        <%         
                                            for (int maxTemp = -10; maxTemp <= 40; maxTemp+=5) {
                                                out.println("<option value='" + maxTemp + "'>" + maxTemp + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Minimum desired rain probaility (%):
                                    <select name="minDesiredRainProb">
                                        <%         
                                            for (int minRain = 0; minRain <= 100; minRain+=5) {
                                                out.println("<option value='" + minRain + "'>" + minRain + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                                <td>
                                    Maximum desired rain probaility (%):
                                    <select name="maxDesiredRainProb">
                                        <%         
                                            for (int maxRain = 0; maxRain <= 100; maxRain+=5) {
                                                out.println("<option value='" + maxRain + "'>" + maxRain + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Minimum desired wind speed (km/h):
                                    <select name="minDesiredWind">
                                        <%         
                                            for (int minWind = 0; minWind <= 10; minWind++) {
                                                out.println("<option value='" + minWind + "'>" + minWind + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                                <td>
                                    Maximum desired wind speed (km/h):
                                    <select name="maxDesiredWind">
                                        <%         
                                            for (int maxWind = 0; maxWind <= 10; maxWind++) {
                                                out.println("<option value='" + maxWind + "'>" + maxWind + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Minimum desired humidity (%):
                                    <select name="minDesiredHumidity">
                                        <%         
                                            for (int minHumidity = 0; minHumidity <= 100; minHumidity+=5) {
                                                out.println("<option value='" + minHumidity + "'>" + minHumidity + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                                <td>
                                    Maximum desired humidity (%):
                                    <select name="maxDesiredHumidity">
                                        <%         
                                            for (int maxHumidity = 0; maxHumidity <= 100; maxHumidity+=5) {
                                                out.println("<option value='" + maxHumidity + "'>" + maxHumidity + "</option>");
                                            }
                                        %>
                                    </select>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
        <%-- ///////////////////////////////////////// --%>
        <div id="ajax"></div>

    </body></html>

